package com.ec.saas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ec.common.annotation.DataSource;
import com.ec.common.constant.TenantConstants;
import com.ec.common.enums.DataSourceType;
import com.ec.common.utils.DateUtils;
import com.ec.saas.domain.MasterTenant;
import com.ec.saas.dto.TenantDatabaseDTO;
import com.ec.saas.mapper.MasterTenantMapper;
import com.ec.saas.service.IMasterTenantService;
import org.springframework.stereotype.Service;

@Service
public class MasterTenantServiceImpl extends ServiceImpl<MasterTenantMapper, MasterTenant> implements IMasterTenantService {

    @Override
    @DataSource(DataSourceType.MASTER)
    public MasterTenant selectMasterTenant(String tenant) {
        LambdaQueryWrapper<MasterTenant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MasterTenant::getTenant, tenant);
        return getOne(queryWrapper);
    }
//
//    @Override
//    @DataSource(DataSourceType.MASTER)
//    public List<MasterTenant> selectMasterTenants(MasterTenant masterTenant) {
//        return masterTenantMapper.selectMasterTenantList(masterTenant);
//    }
//
    @Override
    @DataSource(DataSourceType.MASTER)
    public boolean insertMasterTenant(TenantDatabaseDTO tenantDatabaseDTO) {
        MasterTenant masterTenant = new MasterTenant();
        masterTenant.setTenant(tenantDatabaseDTO.getTenantName());
        masterTenant.setUrl(tenantDatabaseDTO.getUrl());
        masterTenant.setDatabaseName(tenantDatabaseDTO.getTenantDatabase());
        masterTenant.setUsername(tenantDatabaseDTO.getDbUser());
        masterTenant.setPassword(tenantDatabaseDTO.getDbPass());
        masterTenant.setCreateTime(DateUtils.getNowDate());
        masterTenant.setExpirationDate(DateUtils.addDays(DateUtils.getNowDate(), 7));//有效期7天
        return save(masterTenant);
    }
//
//    @Override
//    @DataSource(DataSourceType.MASTER)
//    public int insertMasterTenant(MasterTenant masterTenant) {
//        return masterTenantMapper.insertMasterTenant(masterTenant);
//    }

    /**
     * 查询租户名字是否存在，1 存在 0 不存在
     */
    @Override
    @DataSource(DataSourceType.MASTER)
    public String checkTenantNameUnique(String tenantName) {
        LambdaQueryWrapper<MasterTenant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MasterTenant::getTenant, tenantName);
        return count(queryWrapper) > 0 ? TenantConstants.NOT_UNIQUE : TenantConstants.UNIQUE;
    }
//
//    @Override
//    @DataSource(DataSourceType.MASTER)
//    public int updateMasterTenant(MasterTenant tenant) {
//        return masterTenantMapper.updateMasterTenant(tenant);
//    }

//    @Override
//    @DataSource(DataSourceType.MASTER)
//    public MasterTenant selectMasterTenantById(Long id) {
//        return baseMapper.selectMasterTenantById(id);
//    }
//
//    @Override
//    @DataSource(DataSourceType.MASTER)
//    public int deleteMasterTenantByIds(Long[] ids) {
//        for (Long id : ids) {
//            MasterTenant tenant = selectMasterTenantById(id);
//            if (tenant == null) {
//                throw new ServiceException(String.format("%1$s的数据不存在,不能删除", id));
//            }
//        }
//        return masterTenantMapper.deleteMasterTenantByIds(ids);
//    }
}
